﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：TongTongMallSession.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TongTongMall.Runtime.Security;

namespace TongTongMall.Runtime.Session
{
    public class TongTongMallSession : ITongTongMallSession
    {
        public virtual long? UserID
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var userIdClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
                if (userIdClaim == null || string.IsNullOrEmpty(userIdClaim.Value))
                {

                    return null;
                }

                long userId;
                if (!long.TryParse(userIdClaim.Value, out userId))
                {
                    return null;
                }

                return userId;
            }
        }

        public virtual long? TenantID
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var tenantIdClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.TenantID);
                if (tenantIdClaim == null || string.IsNullOrEmpty(tenantIdClaim.Value))
                {
                    return null;
                }
                long tenantId;
                if (!long.TryParse(tenantIdClaim.Value, out tenantId))
                {
                    return null;
                }
                return tenantId;
            }
        }

        public virtual string TenantName
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var tenantClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.TenantName);
                if (tenantClaim == null || string.IsNullOrEmpty(tenantClaim.Value))
                {
                    return null;
                }
                return tenantClaim.Value;
            }
        }


        public virtual string UserName
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var userClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name);
                if (userClaim == null || string.IsNullOrEmpty(userClaim.Value))
                {
                    return null;
                }
                return userClaim.Value;
            }
        }

        public string PwdKey
        {
            get
            {
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }

                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }

                var userClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == OAuthClaimTypes.TicketVersion);
                if (userClaim == null || string.IsNullOrEmpty(userClaim.Value))
                {
                    return null;
                }
                return userClaim.Value;
            }
        }
    }
}
